package com.mmd.base.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import com.mmd.core.constant.KeyConstant;
import com.mmd.core.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
public class SwaggerConfiguration {
    @Value("${spring.application.name}")
    private String name;

    @Bean
    @Order(1)
    public Docket groupRestApi()
    {
        List<ResponseMessage> responses = Arrays.stream(ResultEnum.values()).map(a -> new ResponseMessageBuilder().code(Integer.parseInt(a.getCode()))
                .responseModel(new ModelRef("非http响应状态,此状态对应ResponseBody中的code字段:"+a.getMessage()))
                .build()
        ).collect(Collectors.toList());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mmd"))
                .paths(PathSelectors.any())
                .build()
                .globalResponseMessage(RequestMethod.GET,responses)
                .globalResponseMessage(RequestMethod.POST,responses)
                .globalResponseMessage(RequestMethod.PUT,responses)
                .globalResponseMessage(RequestMethod.DELETE,responses)
                .securityContexts(Lists.newArrayList(securityContext()))
                .securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
    }

    private ApiInfo groupApiInfo()
    {
        return new ApiInfoBuilder()
                .title(name+"模块")
                .description("<div style='font-size:14px;color:red;'>mmd RESTful Apis</div>")
                .termsOfServiceUrl("#")
//                .contact("2563884165@qq.com")
                .version("1.0")
                .build();
    }

    private List<ApiKey> apiKey() {
        ApiKey apiKey1 = new ApiKey("认证令牌", KeyConstant.AUTHENTICATION, "header");
        ApiKey apiKey2 = new ApiKey("请求来源", KeyConstant.REQUEST_SOURCE, "header");
        ArrayList<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(apiKey1);
        apiKeys.add(apiKey2);
        return apiKeys;
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("认证令牌", authorizationScopes),new SecurityReference("请求来源", authorizationScopes));
    }
}
